//
//  IMStockTools.h
//  Stock_Pro
//
//  Created by imuse on 10-11-26.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#ifndef IM_STOCK_TOOLS
#define IM_STOCK_TOOLS

#include <sys/socket.h>
#include <netdb.h>
#include <pthread.h>

#include "Tools.h"


typedef struct IMSOCKET_TOOLS_tag
{
	// IP地址和端口号
	char				sIP[32];			// ip地址
	int					nPort;			    // 端口号
	
	// Socket相关
	int					iSocket;
	struct sockaddr_in	iSocketAddr;
	
	// 线程
	pthread_t			pConner;
	pthread_t			pSender;           // 发送请求线程
	pthread_t			pReader;           // 接收数据线程
	

	TBOOL				bStop;				//是否已经停止
	
	TBOOL				bLock;				//是否锁定
	// send buf
	char	*pBuf;
	int		nSize;
	int		nLen;
	
	char *pRecv;
	void *pCBObj;
	void(*pCallBack)(struct IMSOCKET_TOOLS_tag *pSocket, int nType);
}IMSOCKET_TOOLS;


IMSOCKET_TOOLS* ImSocketTools_New(char *ip, int port, void(*pCallBack)(IMSOCKET_TOOLS *pSocket, int nType), void *pCBObj);

void* ImSocketTools_Conning(void *lpParam);
void* ImSocketTools_Reading(void *lpParam);
void* ImSocketTools_Sending(void *lpParam);

TBOOL ImSocketTools_Lock( IMSOCKET_TOOLS *pSocket);
TBOOL ImSocketTools_UnLock( IMSOCKET_TOOLS *pSocket);

void ImSocketTools_Send(IMSOCKET_TOOLS *pSocket, char *pBuf, int nLen);

#endif
